package com.ruoyi.web.controller.system;

import cn.dev33.satoken.annotation.SaIgnore;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.domain.tree.PostTreeNode;
import com.ruoyi.system.mapper.SysPostMapper;
import com.ruoyi.system.service.ISysPostService;
import com.ruoyi.system.service.impl.SysPostServiceImpl;
import com.ruoyi.system.util.PostTreeBuild;
import lombok.RequiredArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 岗位信息操作处理
 *
 * @author Lion Li
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/post")
public class SysPostController extends BaseController {

    private final ISysPostService postService;

    private final SysPostServiceImpl sysPostService;

    private final SysPostMapper sysPostMapper;

    /**
     * 获取岗位列表
     */
    @GetMapping("/list")
    public TableDataInfo<SysPost> list(SysPost post, PageQuery pageQuery) {
        return postService.selectPagePostList(post, pageQuery);
    }

    /**
     * 导出岗位列表
     */
    @Log(title = "岗位管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysPost post, HttpServletResponse response) {
        List<SysPost> list = postService.selectPostList(post);
        ExcelUtil.exportExcel(list, "岗位数据", SysPost.class, response);
    }

    /**
     * 根据岗位编号获取详细信息
     *
     * @param postId 岗位ID
     */
    @GetMapping(value = "/{postId}")
    public R<SysPost> getInfo(@PathVariable Long postId) {
        return R.ok(postService.selectPostById(postId));
    }

    /**
     * 新增岗位
     */
    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody SysPost post) {
        return toAjax(postService.insertPost(post));
    }

    /**
     * 修改岗位
     */
    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody SysPost post) {
        return toAjax(postService.updatePost(post));
    }

    /**
     * 删除岗位
     *
     * @param postIds 岗位ID串
     */
    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{postIds}")
    public R<Void> remove(@PathVariable Long postIds) {
        return toAjax(postService.deletePostById(postIds));
    }

    /**
     * 获取岗位选择框列表
     */
    @SaIgnore
    @GetMapping("/optionselect")
    public R<List<SysPost>> optionselect() {
        List<SysPost> posts = postService.selectPostAll();
        return R.ok(posts);
    }

    /**
     * 获取岗位列表
     */
    @GetMapping("/getList")
    public R<List<SysPost>> getList(String deptIds) {
        JSONArray jsonArray = JSONArray.parseArray(deptIds);
        List<Long> list = new ArrayList<>();
        if (!jsonArray.isEmpty()) {
            list = JSONObject.parseArray(jsonArray.toJSONString(), Long.class);
        }
        return R.ok(sysPostService.selectPostByDeptId(list));
    }


    /**
     * 获取部门树列表
     */
    @GetMapping("/postControlTree")
    public R<List<PostTreeNode>> postControlTree(String deptIds) {
        List<Long> ids = new ArrayList<>();
        if (deptIds.contains("[")) {
            JSONArray jsonArray = JSONArray.parseArray(deptIds);
            if (!jsonArray.isEmpty()) {
                ids = JSONObject.parseArray(jsonArray.toJSONString(), Long.class);
            }
        } else {
            ids.add(Long.parseLong(deptIds));
        }
        List<SysPost> postList = sysPostService.selectPostByDeptId(ids);
        List<PostTreeNode> list = Lists.newArrayList();
        for (SysPost sysPost : postList) {
            PostTreeNode postTreeNode = new PostTreeNode(sysPost.getPostId(), "", sysPost.getSuperiorsId() == null ? 0 : sysPost.getSuperiorsId(), sysPost.getPostName(), null, null);
            list.add(postTreeNode);
        }
        // 创建树形结构（数据集合作为参数）
        PostTreeBuild treeBuild = new PostTreeBuild(list);
        // 原查询结果转换树形结构
        list = treeBuild.buildTree();
        return R.ok("操作成功", list);
    }
}
